package com.example.mydemo.company.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mydemo.company.entity.Company;
import com.example.mydemo.company.entity.CompanyAndTypeNature;
import com.example.mydemo.company.entity.CompanyPage;
import com.example.mydemo.company.mapper.CompanyMapper;
import com.example.mydemo.company.service.ICompanyService;
import com.example.mydemo.companyNature.service.ICompanyNatureService;
import com.example.mydemo.companyType.service.ICompanyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Autowired
    private ICompanyTypeService companyTypeService;

    @Autowired
    private ICompanyNatureService companyNatureService;

    @Override
    public List<Company> select() {
        return list();
    }

    @Override
    public Page<CompanyAndTypeNature> page(Integer current, Integer size, String username) {
        //组织map数据
        Map<Integer, String> type_id_name = companyTypeService.idName();
        Map<Integer, String> nature_id_name = companyNatureService.idName();
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        Page<Company> page = page(new Page<>(current, size), wrapper);

       List<CompanyAndTypeNature> CompanyAndTypeNature = new ArrayList<>();
        for (Company company : page.getRecords()){
            CompanyAndTypeNature companyPageEveryData = BeanUtil.copyProperties(company, CompanyAndTypeNature.class);
            companyPageEveryData.setNature_name(type_id_name.get(company.getType()));
            companyPageEveryData.setType_name(nature_id_name.get(company.getNature()));
            CompanyAndTypeNature.add(companyPageEveryData);
        }
        Page<CompanyAndTypeNature> recompanyPageDataRes = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        recompanyPageDataRes.setRecords(CompanyAndTypeNature);
        return recompanyPageDataRes;
    }


}
